package apis

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/api"
	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"gitee.com/sansaniot/ssiot-core/serrors"
	"github.com/gin-gonic/gin"
	"ssadmin/common/constant"
	"ssadmin/internal/admin/models"
	"ssadmin/internal/admin/models/command"
	"ssadmin/internal/admin/models/query"
	"ssadmin/internal/admin/service"
)

type UserApp struct {
	api.Api
}

// FindList 获取用户应用列表
func (e UserApp) FindList(c *gin.Context) {
	req := query.UserAppQuery{}
	s := service.UserApp{}
	err := e.MakeContext(c).
		Bind(&req).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	//获取当前用户对应的租户id
	agencyId := user.Get(c, constant.CacheKeyAgencyId)
	if agencyId != nil {
		req.AgentId = agencyId.(string)
	}

	//查询对应的租户管理的应用列表
	list := make([]models.UserApp, 0)
	var count int64
	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}

	//分页返回
	e.Page(count, req.GetPageSize(), req.GetPageIndex(), list)
}

//明细查询
func (e UserApp) GetDetail(c *gin.Context) {
	req := query.UserAppGetReq{}
	u := models.UserApp{}

	err := e.MakeContext(c).Bind(&req).MakeModel(&u.Model).Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	if err = u.GetDetail(&req); err != nil {
		e.Fail(serrors.Fail)
		return
	}

	e.Data(&u)
}

// Insert 新增应用信息
func (e UserApp) Insert(c *gin.Context) {
	req := command.UserAppInsertReq{}
	s := service.UserApp{}

	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.InvalidParam)
		return
	}

	//获取当前用户对应的创建人
	curCreateId := user.Get(c, constant.CacheKeyCreatorId).(string)
	//租户用户才可以新增
	if curCreateId != "1" {
		e.Fail(0, "租户用户才可以新增应用")
		return
	}

	//获取扩展属性（自定义扩展）
	userId := user.Get(c, constant.CacheKeyUserId).(string)
	req.Creator = user.Get(c, constant.CacheKeyUserName).(string)
	req.CreatorId = userId

	err = s.Insert(&req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}

	e.Ok(nil)
}

// Update 修改应用信息
func (e UserApp) Update(c *gin.Context) {
	req := command.UserAppUpdateReq{}
	s := service.UserApp{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	//获取当前用户对应的创建人
	curCreateId := user.Get(c, constant.CacheKeyCreatorId).(string)
	//租户用户才可以修改
	if curCreateId != "1" {
		e.Fail(0, "租户用户才可以修改应用")
		return
	}

	err = s.Update(&req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}

	e.Ok(nil)
}

// Delete 删除应用信息
func (e UserApp) Delete(c *gin.Context) {
	s := service.UserApp{}
	req := query.UserAppDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	//租户用户才可以删除
	curCreateId := user.Get(c, constant.CacheKeyCreatorId).(string)
	//租户用户才可以修改
	if curCreateId != "1" {
		e.Fail(0, "租户用户才可以删除应用")
		return
	}

	req.CurrentUserId = user.Get(c, constant.CacheKeyUserId).(string)
	err = s.Remove(&req)
	if err != nil {
		e.Fail(0, err.Error())
		return
	}

	e.Ok(nil)
}
